<template>
  <div class="app-container">
    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input placeholder="请输入用户名..." v-model="queryForm.query" clearable></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="initVideoList">搜索</el-button>
      <el-button type="success" :icon="DocumentAdd" @click="handleDialogValue()">新增</el-button>
      <el-popconfirm title="您确定批量删除这些记录吗？" @confirm="handleDelete(null)">
        <template #reference>
          <el-button type="danger" :disabled="delBtnStatus" :icon="Delete">批量删除</el-button>
        </template>
      </el-popconfirm>
    </el-row>
    <el-table :data="tableData" stripe style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"/>
      <el-table-column prop="cover" label="视频封面" width="120" align="center">
        <template v-slot="scope">
          <img :src="scope.row.cover" width="108" height="72"/>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="视频名称" width="100" align="center"/>
      <el-table-column prop="info" label="视频简介" width="400" align="center"/>
      <el-table-column prop="createTime" label="创建时间" width="200" align="center"/>
      <el-table-column prop="updateTime" label="更新时间" width="200" align="center"/>
      <el-table-column prop="remark" label="备注"/>
      <el-table-column prop="action" label="操作" width="400" fixed="right" align="center">
        <template v-slot="scope">
          <el-button type="primary" :icon="Edit" @click="handleDialogValue(scope.row.id)"/>
          <el-popconfirm title="您确定要删除这条记录吗？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button type="danger" :icon="Delete"/>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        v-model:currentPage="queryForm.pageNum"
        v-model:page-size="queryForm.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>
  <Dialog v-model="dialogVisible" :dialogVisible="dialogVisible" :id="id" :dialogTitle="dialogTitle"
          @initVideoList="initVideoList"/>
</template>

<script setup>
import { ElMessage, ElMessageBox } from 'element-plus'
import {Search, Delete, DocumentAdd, Edit, Tools, RefreshRight} from '@element-plus/icons-vue'
import requestUtil, {getServerUrl} from "@/util/request";
import Dialog from './components/dialog'
import {ref} from 'vue'

const tableData = ref([]);
const total = ref(0)
const queryForm = ref({
  query: '',
  pageNum: 1,
  pageSize: 10
})

const initVideoList = async () => {
  const res = await requestUtil.post("bsns/video/list", queryForm.value);
  tableData.value = res.data.videoList;
  console.log(res.data.videoList)
  total.value = res.data.total;
}

initVideoList()

const handleSizeChange = (pageSize) => {
  queryForm.value.pageNum = 1;
  queryForm.value.pageSize = pageSize;
  initVideoList()
}
const handleCurrentChange = (pageNum) => {
  queryForm.value.pageNum = pageNum;
  initVideoList()
}


const dialogVisible = ref(false)
const dialogTitle = ref("")
const id = ref(-1)
const handleDialogValue = (videoId) => {
  if (videoId) {
    id.value = videoId;
    dialogTitle.value = "视频修改"
  } else {
    id.value = -1;
    dialogTitle.value = "视频添加"
  }
  dialogVisible.value = true
}

const delBtnStatus = ref(true)

const multipleSelection = ref([])

const handleSelectionChange = (selection) => {
  console.log("勾选了")
  console.log(selection)
  multipleSelection.value = selection;
  delBtnStatus.value = selection.length === 0;
}

const handleDelete = async (id) => {
  let ids = []
  if (id) {
    ids.push(id)
  } else {
    multipleSelection.value.forEach(row => {
      ids.push(row.id)
    })
  }
  const res = await requestUtil.post("bsns/video/delete", ids)
  if (res.data.code === 200) {
    ElMessage({
      type: 'success',
      message: '执行成功!'
    })
    initVideoList()
  } else {
    ElMessage({
      type: 'error',
      message: res.data.msg,
    })
  }
}
</script>

<style scoped>

</style>
